package com.ecoolex.ying.space.api.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.annotation.Description;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponPackage;
import com.ecoolex.ying.space.enums.EstateProjectActivityCouponPackageStatus;
import com.ecoolex.ying.space.ws.EstateProjectActivityCouponOrderService;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "地产项目活动代金券查询")
public class EstateProjectActivityCouponGraphql {
	
	@Reference
	private EstateProjectActivityService estateProjectActivityService;
	
	@Reference
	private EstateProjectActivityCouponOrderService couponOrderService;
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "canUsed",description = "能使用")
	public boolean canUsed(@Source EstateProjectActivityCouponPackage source) {
		return EstateProjectActivityCouponPackageStatus.UNUSED.equal(source.getStatus());
	}
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "couponOrder",description = "关联的代金券订单")
	public EstateProjectActivityCouponOrder couponOrder(@Source EstateProjectActivityCouponPackage source,ConsumerMemberAuthority user) {
		return couponOrderService.getByOrderIdAndUserId(source.getOrderId(), user.userId());
	}
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "coupon",description = "关联的代金券")
	public EstateProjectActivityCoupon coupon(@Source EstateProjectActivityCouponPackage source) {
		return estateProjectActivityService.getEstateProjectActivityCoupon(source.getCouponId());
	}
	
	@QueryField(field = "estateProjectActivityCoupon",description = "地产项目活动代金券")
	public EstateProjectActivityCoupon get(@NonNull Integer couponId) {
		return estateProjectActivityService.getEstateProjectActivityCouponOnline(couponId);
	}

	@QueryField(field = "estateProjectActivityCouponList",description = "地产项目活动代金券列表")
	public Pagination<EstateProjectActivityCoupon> query(@NonNull Page page) {
		return estateProjectActivityService.queryEstateProjectActivityCouponOnline(page);
	}
	
	@QueryField(field = "estateProjectActivityCouponPackageList",description = "我的券包列表")
	public Pagination<EstateProjectActivityCouponPackage> couponPackageList(@Description({"使用状态：1-待使用；2-已使用；3-已过期；4-已退款"}) Integer status,
		@NonNull Page page,ConsumerMemberAuthority user){
		user.assertLogin();
		return estateProjectActivityService.couponPackageList(status, user.userId(), page);
	}
	
	@QueryField(field = "estateProjectActivityCouponPackage",description = "我的券详情")
	public EstateProjectActivityCouponPackage couponPackage(@NonNull Integer id,ConsumerMemberAuthority user){
		user.assertLogin();
		return estateProjectActivityService.couponPackage(id,user.userId());
	}
	
	@QueryField(field = "estateProjectActivityCouponPackageByCode",description = "券码查询券详情")
	public EstateProjectActivityCouponPackage getCouponPackageByCode(@NonNull String code) {
		return estateProjectActivityService.getEstateProjectActivityCouponPackageByCode(code);
	}
}
